package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Comment;
import cn.wolfcode.trip.base.domain.Zan;
import cn.wolfcode.trip.base.mapper.CommentMapper;
import cn.wolfcode.trip.base.mapper.ZanMapper;
import cn.wolfcode.trip.base.query.TravelCommentQueryObject;
import cn.wolfcode.trip.base.service.ICommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ZanMapper zanMapper;

    @Override
    public PageInfo<Comment> ListAllComments(TravelCommentQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Comment> list = commentMapper.selectForList(qo);
        for (Comment comment : list) {
            Zan zan = new Zan();
            zan.setType(1L);
            if (UserContext.getCurrentUser() != null) {
                zan.setUserId(UserContext.getCurrentUser().getId());
            }
            zan.setTypeId(comment.getId());
            comment.setStatus(zanMapper.getZanByCommentId(zan));


            Zan zan1 = new Zan();
            zan1.setTypeId(comment.getId());
            zan1.setType(1L);
            comment.setCountZan(zanMapper.getZanNumByTravelId(zan1));
        }
        return new PageInfo<>(list);
    }

    public void save(Comment comment) {
        comment.setCommentTime(new Date());
        comment.setCommentUser(UserContext.getCurrentUser());
        commentMapper.insert(comment);
    }

}
